
// import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI, {Message} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import Particles from 'vue-particles'
//import Particles from "vue2-particles";
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
import qs from 'qs'
import '@/common/js/permission'
import './assets/css/image_css.css'
import './assets/css/icon-size.css'
import './assets/css/tabelname.css'
import './assets/css/global.css'
import './assets/css/seachCss.css'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'


//权限指令
// import '@/common/js/permission'
//百度地图
import BaiduMap from 'vue-baidu-map'
import globalComponents from './components'
import {formatTime,formatTimeNew} from "./utils/timeFormatter";
//地区选择器
import {
  provinceAndCityData,
  pcTextArr,
  regionData,
  pcaTextArr,
  codeToText,
} from "element-china-area-data";


Vue.prototype.$formatDateTime = formatTime;
Vue.prototype.$formatTimeNew = formatTimeNew;
Vue.use(BaiduMap, {
  ak: 'abhCTURfZ4jIP1nopcOBhaRd323M0UlT'
})
//引入axios
import axios from 'axios'
//配置axios的全局基本路径，访问后端接口使用8080
//axios.defaults.baseURL = 'http://localhost:8080/'
 axios.defaults.baseURL='https://www.kongqichong.com:8080//'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios
Vue.prototype.$qs = qs
//列表显示图片的前缀
 Vue.prototype.imgPrefix="https://www.kongqichong.com:8888"

//文件上传路径
Vue.prototype.$uploadURL = "https://www.kongqichong.com:8080/minio/upload";

Vue.prototype.$projectName = "空气虫管理系统";
Vue.prototype.$message = Message
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(globalComponents)
Vue.use(Particles)

//Vue.use(Particles)
//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

//1.axios前置拦截器：每次通过axios发送请求之前，让所有的请求都携带uToken
axios.interceptors.request.use(res => {
  //携带token
  let uToken = localStorage.getItem("token");
  if (uToken) {
    res.headers['token'] = uToken;
  }
  return res;
}, error => {
  Promise.reject(error);
})
let isLogoutPromptShowing = false;
//2.axios后置拦截器：后端处理完axios请求之后，处理响应的数据
//发送axios请求之后都会执行以下代码
axios.interceptors.response.use(res => {
  if (false === res.data.success && "noLogin" === res.data.msg) {

    //删除localStorage的信息
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    // 如果当前没有显示登录失效提示，则显示
    if (!isLogoutPromptShowing) {
      isLogoutPromptShowing = true;
      // 显示登录失效的提示信息
      Message({
        type: 'warning',
        message: '当前登录已失效，稍后将重新登录',
        duration: 3000, // 显示时间
        onClose: () => {
          // 跳转到登录页面
          router.push({ path: '/login' });
        }
      })};
  } else if(false === res.data.success && "timeout" === res.data.msg){
    //删除localStorage的信息
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    //跳转到登录页面
    router.push({path: '/login'});
    // 显示超时的提示信息
    Message({
      type: 'warning',
      message: '当前登录超时，3秒后重新登录',
      duration: 3000, // 显示时间
      onClose: () => {
        // 跳转到登录页面
        router.push({ path: '/login' });
      }
    });
  }
  return res;
},error => {
  Promise.reject(error)
})
//3.路由前置拦截器 - 拦截非axios请求
router.beforeEach((to, from, next) => {
  //如果访问登录页面   或  注册页面
  if (to.path == '/login' || to.path == "/shopRegister") {
    next();//放行
  }else{//其他页面都需要判断你是否登录过
    //获取localStorage的登录信息
    let logininfo = localStorage.getItem('logininfo');
    if (logininfo) {
      next(); //登录过放行
    } else {
      next({path: '/login'});//跳转到login - 登录页面
    }
  }
})
//处理页面刷新动态路由失效问题
initIndexRouters();
function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  //防止重复配置路由：5就是main.js中路由的个数 - 如果你的静态路由是6个这里要写成6
  if(router.options.routes.length>10){
    return;
  }
  let menus = localStorage.getItem('menus');
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  router.addRoutes(tempRouters);
}

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')
